﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Linq;


#endregion

namespace ScrumTable.UI.ViewModel.Tools.Toast
{
    /// <summary>
    /// View Model for a simple menu
    /// </summary>
    public class ViewModelToastScreen : ViewModelBase
    {

        #region Declarations

        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly ToastList _toasts = new ToastList();
        private readonly List<ToastBaseProvider> _providers = new List<ToastBaseProvider>();

        #endregion


        #region Properties

        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        public ToastList Toasts
        {
            get { return _toasts; }
        }

        ///checks if there are no error (example: from save process) in scrumtable
        public bool HasNoError { get { return _toasts.Count(x => x.Type == ToastType.Error) == 0; } }


        #endregion

        #region Constructors / Destructor

        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        public ViewModelToastScreen()
        {
            _toasts.CollectionChanged += OnToastsCollectionChanged;
        }

 
        #endregion

        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        public void AddErrorPublisher()
        {
            if (_providers.FirstOrDefault(item => item.ToastType == ToastType.Error) == null)
            {
                _providers.Add(new ErrorToastProvider(Toasts));
            }
        }
    

        public void RemovePublisher(ToastType type)
        {
            var provider = _providers.FirstOrDefault(item => item.ToastType == type);
            if( provider != null)
            {
                provider.Dispose();
                _providers.Remove(provider);
            }
        }

        protected override void ReleaseManagedResources()
        {
            _toasts.CollectionChanged -= OnToastsCollectionChanged;

            foreach (var provider in _providers)
            {
                provider.Dispose();
            }
            base.ReleaseManagedResources();
        }

        #endregion

        #region Events

        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------



        void OnToastsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            SendPropertyChanged("HasNoError");
        }


        #endregion

    }
}